package day2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Day02_03 {
    public static void main(String[] args) throws IOException {
        /*Scanner in = new Scanner(System.in);
        int len = in.nextInt();
        String str1 = in.next();
        String str2 = in.next();

        if(str1 == null || str2 == null) System.out.println(-1);

        String[] strs = new String[len];
        for(int i = 0; i < len; i++) strs[i] = in.next();*/
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int len = Integer.parseInt(reader.readLine());
        String[] strs = reader.readLine().split(" ");
        String str1 = strs[0], str2 = strs[1];

        int ret = Integer.MAX_VALUE;

        int flag0 = 0;
        int flag1 = 0;
        for(int i = 2; i < len; i++) {
            if(strs[i].equals(str1) ) {
                flag0 = i;
            }
            if(strs[i].equals(str2)) {
                flag1 = i;
            }
            if(ret == 0 || ret > Math.abs(flag1 - flag0)) ret =  Math.abs(flag1 - flag0);
        }
        if(flag0 == 0 || flag1 == 0) System.out.println(-1);
        else System.out.println(ret);
    }
}
